﻿/*
 * Created by SharpDevelop.
 * User: Chris
 * Date: 6/10/2013
 * Time: 5:52 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.ComponentModel;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Project;

namespace Dither.Xna.Bindings.Project
{
    /// <summary>
    /// Description of XnaContentAssetNameProperty.
    /// </summary>
    public class XnaContentAssetNameProperty : PropertyDescriptor
    {
        public XnaContentAssetNameProperty()
            : base("Name", null)
        {
            displayName = "Asset Name";
        }

        internal string displayName, description;
        internal bool runCustomTool;
        
        internal string m_resetValue;

        public override string DisplayName
        {
            get
            {
                if (string.IsNullOrEmpty(displayName))
                {
                    return Name;
                }
                else
                {
                    return StringParser.Parse(displayName);
                }
            }
        }

        public override string Description
        {
            get { return StringParser.Parse(description); }
        }

        public override Type ComponentType
        {
            get { return typeof(ProjectItem); }
        }

        public override bool IsReadOnly
        {
            get { return false; }
        }

        public override Type PropertyType
        {
            get { return typeof(string); }
        }

        public override bool CanResetValue(object component)
        {
            return true;
        }

        public override object GetValue(object component)
        {
            return ((ProjectItem)component).GetEvaluatedMetadata(Name);
        }

        public override void ResetValue(object component)
        {
        	string componentString = (string)component;
        	if (!string.IsNullOrEmpty(componentString))
        	{
            	SetValue(component, null);
        	}
        	else
        	{
        		SetValue(m_resetValue, null);
        	}
        }

        public override void SetValue(object component, object value)
        {
            ProjectItem p = (ProjectItem)component;
            p.SetEvaluatedMetadata(Name, (string)value);
            p.InformSetValue(this, component, value);
            if (runCustomTool)
            {
                FileProjectItem fpi = p as FileProjectItem;
                if (fpi != null)
                {
                    CustomToolsService.RunCustomTool(fpi, false);
                }
            }
        }

        public override bool ShouldSerializeValue(object component)
        {
            return !string.IsNullOrEmpty((string)GetValue(component));
        }
    }
}
